﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DnD.UI.Controls;
using DnD.UI.Configuration;
using DnD.Core.Campaign;
using DnD.UI.Panels;

namespace DnD.UI.Screens
{
    public partial class MainScreen : Form
    {
        private Setting _setting;
        private BasePanel _activePanel;
        
        public MainScreen()
        {
            _setting = new Setting();
            _activePanel = null;
            PrepareTestSetting();
            InitializeComponent();
            InitializeNavigator();

            split_main.Panel2.SizeChanged += OnResizePanel;
        }
        private void PrepareTestSetting()
        {
            Character gnobbnock = new Character()
            {
                Name = "Gnobbnock"
                ,
                CharacterLevel = 1
                ,
                ClassNames = "Schurke(1)"
                ,
                RaceName = "Gnom"
            };
            gnobbnock.Attributes.ST = 12;
            gnobbnock.Attributes.GE = 16;
            gnobbnock.Attributes.KO = 10;
            gnobbnock.Attributes.IN = 13;
            gnobbnock.Attributes.WE = 12;
            gnobbnock.Attributes.CH = 15;

            Character rurak = new Character()
            {
                Name = "Rurak"
                ,
                CharacterLevel = 1
                ,
                ClassNames = "Schurke(1)"
                ,
                RaceName = "Gnom"
            };
            rurak.Attributes.ST = 10;
            rurak.Attributes.GE = 17;
            rurak.Attributes.KO = 10;
            rurak.Attributes.IN = 15;
            rurak.Attributes.WE = 13;
            rurak.Attributes.CH = 9;



            Player dave = new Player()
            {
                Name = "Dave"
            };
            dave.Characters.Add(gnobbnock);
            dave.Characters.Add(rurak);
            dave.Characters.Add(rurak);
            _setting.Group.Players.Add(dave);

        }
        private void InitializeNavigator()
        {
            NavigatorConfiguration navConfig = new NavigatorConfiguration();
            navConfig.LoadSchema(UIConfiguration.PATH_NAVIGATION_SCHEMA);
            Navigator n = new Navigator(navConfig);
            n.Dock = DockStyle.Fill;
            split_main.Panel1.Controls.Add(n);
            n.NodeSelected += OnNavigatorNodeSelect;

            n.TriggerFirstNode();
        }
        private bool CanProceed()
        {
            if (_activePanel == null)
                return true;
            if (_activePanel.Info.DataHasBeenModified && !_activePanel.Info.ModificationsSaved)
                return false;
            return true;
        }
        private void OnNavigatorNodeSelect(object sender, NavigatorEventArgs args)
        {
            info_bar_main.Text = args.Item.FullID;
            NavigatorItem item = args.Item;
            if (item.FullID == "Overview")
            {
                if(!CanProceed())
                {
                    if(!DiscardChanges())
                        return;
                }
                Overview gip = PanelFactory.GetOverviewInformationPanel(_setting.Group);
                _activePanel = gip;
                //Do actions to check for unsaved items
            }
            else if (item.FullID == "Group")
            {
                if(!CanProceed())
                {
                    if(!DiscardChanges())
                        return;
                }
                GroupInformation gip = PanelFactory.GetGroupInformationPanel(_setting.Group);
                _activePanel = gip;
            
            }
            _activePanel.Dock = DockStyle.Fill;
            _activePanel.Width = split_main.Panel2.Width;
            _activePanel.Height = split_main.Panel2.Height;
            split_main.Panel2.Controls.Clear();
            split_main.Panel2.Controls.Add(_activePanel);
            Restack();
        }
        public void Restack()
        {
            GroupInformation gi = _activePanel as GroupInformation;
            if (gi != null)
                gi.RestackChildControls();
            return;
        }
        private void OnResizePanel(object sender, EventArgs e)
        {
            Panel p = sender as Panel;
            if (p != null && p.Controls.Count > 1)
            {
                p.Controls[1].Width = p.Width;
            }
                Restack();
        }
        private bool DiscardChanges()
        {
            DialogResult res = MessageBox.Show("Data has been modified. Changes will be discarded if you continue", "Discard changed", MessageBoxButtons.OKCancel);
            return res == DialogResult.OK; 
        }
    }
}
